package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.enums.LindiProvinceIndexEnum;
import com.minimalist.common.entity.lindi.po.LindiProvinceIndex;
import com.minimalist.common.entity.lindi.vo.provinceindex.LindiProvinceIndexQueryVO;
import com.minimalist.common.entity.lindi.vo.provinceindex.LindiProvinceIndexVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiProvinceIndexMapper;
import com.minimalist.lindi.service.LindiProvinceIndexService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 林地指标交易平台_省级公益林指标 服务层实现。
 *
 * @author 11987
 * @since 2025-05-13
 */
@Service
public class LindiProvinceIndexServiceImpl extends ServiceImpl<LindiProvinceIndexMapper, LindiProvinceIndex> implements LindiProvinceIndexService{
    private static final Logger log = LoggerFactory.getLogger(LindiProvinceIndexServiceImpl.class);


    @Autowired
    private LindiProvinceIndexMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProvinceIndex(LindiProvinceIndexVO indexVO){
        LindiProvinceIndex provinceIndex = BeanUtil.copyProperties(indexVO, LindiProvinceIndex.class);
        provinceIndex.setProvinceIndexId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(provinceIndex, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                LindiProvinceIndexEnum.ErrorMsgEnum.ADD_INDEX_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProvinceIndexByProvinceIndexId(Long provinceIndexId){
        LindiProvinceIndex provinceIndex = mapper.selectProvinceIndexByProvinceIndexId(provinceIndexId);
        Assert.notNull(provinceIndex, () -> new BusinessException(
                LindiProvinceIndexEnum.ErrorMsgEnum.NONENTITY_INDEX.getDesc()));
        int delete = mapper.delete(provinceIndex);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                LindiProvinceIndexEnum.ErrorMsgEnum.DELETE_INDEX_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProvinceIndexByProvinceIndexId(LindiProvinceIndexVO indexVO){
        LindiProvinceIndex provinceIndex = mapper.selectProvinceIndexByProvinceIndexId(indexVO.getProvinceIndexId());
        Assert.notNull(provinceIndex, () -> new BusinessException(
                LindiProvinceIndexEnum.ErrorMsgEnum.NONENTITY_INDEX.getDesc()));
        BeanUtil.copyProperties(indexVO, provinceIndex, CopyOptions.create().ignoreNullValue());
        int update = mapper.update(provinceIndex);
        Assert.isTrue(update == 1, () -> new BusinessException(
                LindiProvinceIndexEnum.ErrorMsgEnum.UPDATE_INDEX_ERROR.getDesc()));
    }

    @Override
    public PageResp<LindiProvinceIndexVO> getPageProvinceIndexList(LindiProvinceIndexQueryVO queryVO){
        //分页查询
        Page<LindiProvinceIndex> indexPage = mapper.selectPageProvinceIndexList(queryVO);
        //数据转换
        List<LindiProvinceIndexVO> indexVOList = BeanUtil.copyToList(indexPage.getRecords(), LindiProvinceIndexVO.class);
        return new PageResp<>(indexVOList, indexPage.getTotalRow());
    }

    @Override
    public LindiProvinceIndexVO getProvinceIndexByProvinceIndexId(Long provinceIndexId){
        LindiProvinceIndex provinceIndex = mapper.selectProvinceIndexByProvinceIndexId(provinceIndexId);
        Assert.notNull(provinceIndex, () -> new BusinessException(
                LindiProvinceIndexEnum.ErrorMsgEnum.NONENTITY_INDEX.getDesc()));
        return BeanUtil.copyProperties(provinceIndex, LindiProvinceIndexVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initProvinceIndex(){
        // 查询是否已存在省级公益林指标数据
        long count = mapper.selectCountByQuery(QueryWrapper.create());
        LocalDateTime now = LocalDateTime.now();
        if (count == 0){
            log.info("开始初始化省级公益林指标数据");
            // 创建省级公益林指标
            LindiProvinceIndex provinceIndex = new LindiProvinceIndex();
            provinceIndex.setProvinceIndexId(UnqIdUtil.uniqueId());
            provinceIndex.setProvinceQuantity("500");
            provinceIndex.setTradeQuantity("0");
            provinceIndex.setCreateId(0L);
            provinceIndex.setCreateTime(now);
            provinceIndex.setUpdateId(0L);
            provinceIndex.setUpdateTime(now);

            // 插入数据
            int insert = mapper.insert(provinceIndex, true);
            Assert.isTrue(insert == 1, () -> new BusinessException(
                    LindiProvinceIndexEnum.ErrorMsgEnum.ADD_INDEX_ERROR.getDesc()));
            log.info("成功初始化省级公益林指标数据");
        } else{
            log.info("省级公益林指标数据已存在，无需初始化");
        }
    }
}

